﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class InvoiceListControl : RSBaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void gvInvoiceList_Select(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyId = userIdentity.RSCompanyId;
            int invoiceid = Convert.ToInt32(e.CommandArgument.ToString());

            if (companyId > 0 && invoiceid > 0)
                Response.Redirect("RSInvoiceDetail.aspx?invoiceid=" + invoiceid);
        }

        protected void lnkDeleteInvoice_Click(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int invoiceid = Convert.ToInt32(e.CommandArgument.ToString());

                if (DeleteInvoice(companyid, invoiceid))
                    ucModalActionMessageControl.ShowModal("Invoice deleted successfully!", Request.RawUrl, true);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        private bool DeleteInvoice(int companyid, int invoiceid)
        {
            bool returnValue = false;
            if (companyid > 0 && invoiceid > 0)
            {
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@invoiceid", SqlDbType.Int, 0, ParameterDirection.Input, invoiceid);
                SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspDeleteInvoiceByID");
                SQLHelper.ExecuteScalarCmd(sqlCmd);
                returnValue = Convert.ToBoolean(sqlCmd.Parameters["@ReturnValue"].Value);
            }
            return returnValue;
        }
        protected void lnkViewInvoiceReport_Click(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            int invoiceid = Convert.ToInt32(e.CommandArgument.ToString());

            if (companyid > 0 && invoiceid > 0)
                Response.Redirect("RSInvoiceReport.aspx?invoiceid=" + invoiceid);
        }
        public void LoadInvoiceDataByWorkOrderID(int companyid, int WorkOrderid)
        {
            lblInvoiceHistory.Text = "WorkOrder Invoice";
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "SELECT i.InvoiceId,i.WorkOrderTotalMaterialCost_CF,i.WorkOrderTotalLaborCost_CF,i.WorkOrderTotalCost_CF,i.AdditionalFeeAmount,i.AdditionalDiscountAmount,i.BalanceDue_CF,(select s.statusname from rs_tblStatus s where s.StatusId=i.statusid) AS Status,i.WorkOrderId,i.LocationId,i.BillingContactId,i.CompanyId,i.CreateDT,i.DueDT,i.SentDT,i.Comment FROM rs_tblInvoice i WHERE i.workorderid=@WorkOrderId AND i.CompanyId=@CompanyId";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@WorkOrderId";
            param.Value = WorkOrderid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvInvoiceList.DataSource = ds;
            gvInvoiceList.DataBind();
            myConnection.Close();

        }

        public void LoadInvoiceDataByLocationID(int companyid, int locationid)
        {
            lblInvoiceHistory.Text = "Location Invoice History";
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "SELECT i.InvoiceId,i.WorkOrderTotalMaterialCost_CF,i.WorkOrderTotalLaborCost_CF,i.WorkOrderTotalCost_CF,i.AdditionalFeeAmount,i.AdditionalDiscountAmount,i.BalanceDue_CF,(select s.statusname from rs_tblStatus s where s.StatusId=i.statusid) AS Status,i.WorkOrderId,i.LocationId,i.BillingContactId,i.CompanyId,i.CreateDT,i.DueDT,i.SentDT,i.Comment FROM rs_tblInvoice i WHERE i.LocationId=@LocationId AND i.CompanyId=@CompanyId";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@locationid";
            param.Value = locationid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvInvoiceList.DataSource = ds;
            gvInvoiceList.DataBind();
            myConnection.Close();

        }
        public void LoadInvoiceDataByAll(int companyid)
        {
            lblInvoiceHistory.Text = "Invoices";
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "SELECT i.InvoiceId,i.WorkOrderTotalMaterialCost_CF,i.WorkOrderTotalLaborCost_CF,i.WorkOrderTotalCost_CF,i.AdditionalFeeAmount,i.AdditionalDiscountAmount,i.BalanceDue_CF,(select s.statusname from rs_tblStatus s where s.StatusId=i.statusid) AS Status,i.WorkOrderId,i.LocationId,i.BillingContactId,i.CompanyId,i.CreateDT,i.DueDT,i.SentDT,i.Comment FROM rs_tblInvoice i WHERE i.CompanyId=@CompanyId order by 1";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            Session["DATATABLE"] = ds.Tables[0];
            gvInvoiceList.DataSource = ds;
            gvInvoiceList.DataBind();
            myConnection.Close();
        }
        private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }
            return newSortDirection;
        }

        protected void gvInvoiceList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvInvoiceList.PageIndex = e.NewPageIndex;
            gvInvoiceList.DataBind();
        }

        protected void gvInvoiceList_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = Session["DATATABLE"] as DataTable;
            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                gvInvoiceList.DataSource = dataView;
                gvInvoiceList.DataBind();
            }
        }

    }

    
}